//
//  TwoMainView.swift
//  GomroClient
//
//  Created by xiaogxjkz on 2021/4/13.
//  Copyright © 2021 cn.gomro.app. All rights reserved.
//

import UIKit
import Charts
import YYKit

class TwoMainView: BaseView {
    
    private var viewModel:MainViewModel?
    
    /// 行情数据
    private lazy var arrChartData = [[String:Any]]()
    
    
    //MARK: - override
    override init(ViewModel vm: ViewModelProtocol) {
        self.viewModel = vm as? MainViewModel
        super.init(ViewModel: vm)
        
        //加载K线
        self.viewModel?.loadMin5DataCommand.execute(true)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func bindViewModel() {
        
        self.viewModel?.loadMin5DataCommand.executionSignals.switchToLatest()?.subscribeNext({[unowned self] (input:Any?) in
            //行情数据
            if let _arrTemp = input as? [[String:Any]],_arrTemp.count > 0 {
                self.arrChartData = _arrTemp
            }
            
            DispatchQueue.main.async {
                self.setMainCombineLineData(isChange: true)
                self.setCombineLineData(isChange: true)
            }
        })
    }
    
    override func setupViews() {
        //顶部指标
        self.addSubview(self.headView)
        self.headView.addSubview(self.labOpen)
        self.headView.addSubview(self.labHeight)
        self.headView.addSubview(self.labLow)
        self.headView.addSubview(self.labClose)
        
        //主图
        self.addSubview(self.combineLineStickChart)
        self.combineLineStickChart.addSubview(self.btnMATitle)
        self.combineLineStickChart.addSubview(self.topView)
        self.combineLineStickChart.addSubview(self.topTagView)
        
        //副图
        self.addSubview(self.combineLineChare)
        self.combineLineChare.addSubview(self.btnMACDTitle)
        self.combineLineChare.addSubview(self.bottomView)
        self.combineLineChare.addSubview(self.bottomTagView)
        
        //十字线
        self.addSubview(self.labVerticalLine)
        self.addSubview(self.labHorizontalLine)
        self.addSubview(self.labHorizontalRight)
        
        //添加手势
        let longGest = UILongPressGestureRecognizer.init(target: self, action: #selector(longGestAction(sender:)))
        longGest.minimumPressDuration = TimeInterval.init(1.0)
        self.addGestureRecognizer(longGest)
        
        setNeedsUpdateConstraints()
    }
    
    
    override func updateConstraints() {
        
        self.headView.snp.makeConstraints { make in
            make.left.right.top.equalTo(0)
            make.height.equalTo(45)
        }
        
        self.labOpen.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.width.equalTo(Setting.K_APP_WIDTH / 4)
            make.bottom.equalTo(-5)
            make.height.equalTo(21)
        }
        
        self.labHeight.snp.makeConstraints { make in
            make.left.equalTo(Setting.K_APP_WIDTH / 4)
            make.width.equalTo(Setting.K_APP_WIDTH / 4)
            make.bottom.equalTo(-5)
            make.height.equalTo(21)
        }
        
        self.labLow.snp.makeConstraints { make in
            make.left.equalTo(Setting.K_APP_WIDTH / 2)
            make.width.equalTo(Setting.K_APP_WIDTH / 4)
            make.bottom.equalTo(-5)
            make.height.equalTo(21)
        }
        
        self.labClose.snp.makeConstraints { make in
            make.left.equalTo(Setting.K_APP_WIDTH / 4 * 3)
            make.width.equalTo(Setting.K_APP_WIDTH / 4)
            make.bottom.equalTo(-5)
            make.height.equalTo(21)
        }
        
        self.combineLineStickChart.snp.makeConstraints { [weak self] make in
            guard let self = self else { return }
            make.top.equalTo(self.headView.snp.bottom)
            make.left.right.equalTo(0)
            make.height.equalTo(400)
        }
        
        self.btnMATitle.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.top.equalTo(0)
            make.width.equalTo(90)
            make.height.equalTo(18)
        }
        
        self.topTagView.snp.makeConstraints {[weak self] make in
            guard let self = self else { return }
            make.left.equalTo(self.btnMATitle.snp.left)
            make.right.equalTo(self.btnMATitle.snp.right).offset(-20)
            make.height.equalTo(MarketSetting.arrMainKPI.count * 30)
            make.top.equalTo(self.btnMATitle.snp.bottom)
        }
        
        self.topView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(0)
            make.right.equalTo(-15)
            make.height.equalTo(18)
        }
        
        self.combineLineChare.snp.makeConstraints {[weak self] make in
            guard let self = self else { return }
            make.left.right.equalTo(0)
            make.top.equalTo(self.combineLineStickChart.snp.bottom)
            make.height.equalTo(120)
        }
        
        self.btnMACDTitle.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.top.equalTo(0)
            make.width.equalTo(90)
            make.height.equalTo(18)
        }
        
        self.bottomTagView.snp.makeConstraints { [weak self] make in
            guard let self = self else { return }
            make.left.equalTo(self.btnMACDTitle.snp.left)
            make.right.equalTo(self.btnMACDTitle.snp.right).offset(-20)
            make.height.equalTo(MarketSetting.arrMinorKPI.count * 30)
            make.top.equalTo(self.btnMACDTitle.snp.bottom)
        }
        
        self.bottomView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(0)
            make.right.equalTo(-10)
            make.height.equalTo(18)
        }
        
        self.labVerticalLine.snp.makeConstraints { [weak self] make in
            guard let self = self else { return }
            make.width.equalTo(0.5)
            make.right.equalTo(0)
            make.top.equalTo(10)
            make.bottom.equalTo(self.combineLineChare.snp.bottom).offset(-10)
        }
        
        self.labHorizontalLine.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.right.equalTo(-30)
            make.height.equalTo(0.5)
            make.top.equalTo(0)
        }
        
        self.labHorizontalRight.snp.makeConstraints { make in
            make.width.equalTo(30)
            make.right.equalTo(0)
            make.height.equalTo(15)
            make.top.equalTo(0)
        }
        
        super.updateConstraints()
    }
    
    
    //MARK: - lazy load
    /// 垂直线
    private lazy var labVerticalLine:YYLabel = {
        autoreleasepool {
            let _line = BaseView.createLable(rect: .zero,
                                             text: nil,
                                             textColor: nil,
                                             font: nil,
                                             backgroundColor:MarketSetting.highlightColor)
            _line.isHidden = true
            return _line
        }
    }()
    
    /// 水平线右边值
    private lazy var labHorizontalRight:YYLabel = {
        autoreleasepool {
            let _line = BaseView.createLable(rect: .zero,
                                             text: "0.00",
                                             textColor: UIColor.init().colorFromHexInt(hex: 0xffffff, alpha: 0.6),
                                             font: UIFont.systemFont(ofSize: 10),
                                             backgroundColor:MarketSetting.highlightColor)
            _line.textAlignment = .left
            _line.isHidden = true
            return _line
        }
    }()
    
    /// 水平线
    private lazy var labHorizontalLine:YYLabel = {
        autoreleasepool {
            let _line = BaseView.createLable(rect: .zero,
                                             text: nil,
                                             textColor: nil,
                                             font: nil,
                                             backgroundColor:MarketSetting.highlightColor)
            _line.isHidden = true
            return _line
        }
    }()
    
    //MARK: -
    /// 顶部工具视图
    private lazy var headView:UIView = {
        autoreleasepool {
            let _v = BaseView.createView(rect: .zero,backgroundColor: UIColor.lightGray)
            
            //开盘
            let _ot = BaseView.createLable(rect: .zero,
                                           text: "开盘",
                                           textColor: UIColor.init().colorFromHexInt(hex: 0xffffff, alpha: 0.6),
                                           font: UIFont.systemFont(ofSize: 12))
            _ot.textAlignment = .center
            _v.addSubview(_ot)
            _ot.snp.makeConstraints { make in
                make.left.equalTo(0)
                make.width.equalTo(Setting.K_APP_WIDTH / 4)
                make.top.equalTo(8)
                make.height.equalTo(15)
            }
            
            //最高
            let _oh = BaseView.createLable(rect: .zero,
                                           text: "最高",
                                           textColor: UIColor.init().colorFromHexInt(hex: 0xffffff, alpha: 0.6),
                                           font: UIFont.systemFont(ofSize: 12))
            _oh.textAlignment = .center
            _v.addSubview(_oh)
            _oh.snp.makeConstraints { make in
                make.left.equalTo(Setting.K_APP_WIDTH / 4)
                make.width.equalTo(Setting.K_APP_WIDTH / 4)
                make.top.equalTo(8)
                make.height.equalTo(15)
            }
            
            //最低
            let _ol = BaseView.createLable(rect: .zero,
                                           text: "最低",
                                           textColor: UIColor.init().colorFromHexInt(hex: 0xffffff, alpha: 0.6),
                                           font: UIFont.systemFont(ofSize: 12))
            _ol.textAlignment = .center
            _v.addSubview(_ol)
            _ol.snp.makeConstraints { make in
                make.left.equalTo(Setting.K_APP_WIDTH / 2)
                make.width.equalTo(Setting.K_APP_WIDTH / 4)
                make.top.equalTo(8)
                make.height.equalTo(15)
            }
            
            //收盘
            let _oc = BaseView.createLable(rect: .zero,
                                           text: "收盘",
                                           textColor: UIColor.init().colorFromHexInt(hex: 0xffffff, alpha: 0.6),
                                           font: UIFont.systemFont(ofSize: 12))
            _oc.textAlignment = .center
            _v.addSubview(_oc)
            _oc.snp.makeConstraints { make in
                make.left.equalTo(Setting.K_APP_WIDTH / 4 * 3)
                make.width.equalTo(Setting.K_APP_WIDTH / 4)
                make.top.equalTo(8)
                make.height.equalTo(15)
            }
            
            return _v
        }
    }()
    
    //开盘
    private lazy var labOpen:YYLabel = {
        autoreleasepool {
            let _ot = BaseView.createLable(rect: .zero,
                                           text: "0.00",
                                           textColor: UIColor.white,
                                           font: UIFont.systemFont(ofSize: 14))
            _ot.textAlignment = .center
            return _ot
        }
    }()
    
    //最高
    private lazy var labHeight:YYLabel = {
        autoreleasepool {
            let _ot = BaseView.createLable(rect: .zero,
                                           text: "0.00",
                                           textColor: UIColor.white,
                                           font: UIFont.systemFont(ofSize: 14))
            _ot.textAlignment = .center
            return _ot
        }
    }()
    
    //最低
    private lazy var labLow:YYLabel = {
        autoreleasepool {
            let _ot = BaseView.createLable(rect: .zero,
                                           text: "0.00",
                                           textColor: UIColor.white,
                                           font: UIFont.systemFont(ofSize: 14))
            _ot.textAlignment = .center
            return _ot
        }
    }()
    
    //收盘
    private lazy var labClose:YYLabel = {
        autoreleasepool {
            let _ot = BaseView.createLable(rect: .zero,
                                           text: "0.00",
                                           textColor: UIColor.white,
                                           font: UIFont.systemFont(ofSize: 14))
            _ot.textAlignment = .center
            return _ot
        }
    }()
    
    //MARK: 混合主股票蜡烛图
    private lazy var combineLineStickChart:CombinedChartView = {[weak self] in
        autoreleasepool {
            let _stChartView = CombinedChartView.init()
            
            //设置动画时间
            _stChartView.animate(xAxisDuration:MarketSetting.timeInterval)
            
            //整体样式
            MarketSetting.setGradStyleFor(Chart: _stChartView,
                                          withEdgeInsets: .init(top: 10, left: 10, bottom: 0, right: 0),
                                          andDelegate:self)
            
            //底部横轴样式
            MarketSetting.setBottomCalibrationStyleFor(Chart: _stChartView,
                                                       andIsShow: true,
                                                       withShowCount: 0)
            
            //缩放样式
            MarketSetting.setZoomStyleFor(Chart: _stChartView)
            
            //空视图样式
            MarketSetting.setEmptyStyleFor(Chart: _stChartView)
            
            //右边刻度样式
            MarketSetting.setRightCalibrationStyleFor(Chart: _stChartView, andShowCount: 7)
            
            return _stChartView
        }
    }()
    
    /// 主图指标切换
    private lazy var topTagView:UIView = {
        autoreleasepool {
            let _v = BaseView.createView(rect: .zero,
                                         backgroundColor: UIColor.init().colorFromHexInt(hex: 0x000000,alpha: 0.6),
                                         isRadius: false,
                                         cornerRadius: 0,
                                         andBorder: 0,
                                         andBorderColor: nil)
            
            for i in 0..<MarketSetting.arrMainKPI.count {
                let _btn = BaseView.createBtn(rect: .zero,
                                              strTitle: MarketSetting.arrMainKPI[i],
                                              titleColor: UIColor.white,
                                              txtFont: UIFont.systemFont(ofSize: 12),
                                              image: nil,
                                              backgroundColor: nil,
                                              borderColor: nil,
                                              cornerRadius: nil,
                                              isRadius: false,
                                              backgroundImage: nil,
                                              borderWidth: nil)
                
                _btn.tag = i
                _btn.rac_signal(for: .touchUpInside)?.subscribeNext({[weak self] (_btn:Any?) in
                    guard let self = self else { return }
                    guard let btn = _btn as? UIButton else { return }
                    if MarketSetting.mainCurrentIndex == btn.tag {
                        self.topTagView.isHidden = true
                        return
                    }
                    
                    MarketSetting.mainCurrentIndex = btn.tag
                    switch btn.tag {
                    case 0:
                        self.btnMATitle.setTitle("MA(5,10,15,30)", for: .normal)
                        break
                        
                    case 1:
                        self.btnMATitle.setTitle("BOLL(26,2)", for: .normal)
                        break
                        
                    default:
                        self.btnMATitle.setTitle(MarketSetting.arrMainKPI.count > btn.tag ? MarketSetting.arrMinorKPI[btn.tag] : "未知", for: .normal)
                        break
                    }
                    
                    self.topTagView.isHidden = true
                    self.setMainCombineLineData(isChange: true)
                })
                
                _v.addSubview(_btn)
                _btn.snp.makeConstraints { make in
                    make.left.right.equalTo(0)
                    make.height.equalTo(30)
                    make.top.equalTo(_btn.tag * 30)
                }
            }
            
            _v.isHidden = true
            return _v
        }
    }()
    
    //主图MA标题
    private lazy var btnMATitle:UIButton = {
        autoreleasepool {
            let _btn = BaseView.createBtn(rect: .zero,
                                          strTitle: "MA(5,10,15,30)",
                                          titleColor: UIColor.init().colorFromHexInt(hex: 0x333333, alpha: 0.5),
                                          txtFont: UIFont.systemFont(ofSize: 12),
                                          image: nil,
                                          backgroundColor: nil,
                                          borderColor: nil,
                                          cornerRadius: nil,
                                          isRadius: false,
                                          backgroundImage: nil,
                                          borderWidth: nil)
            
            _btn.contentHorizontalAlignment = .left
            
            _btn.rac_signal(for: .touchUpInside)?.subscribeNext({ (_:Any?) in
                self.topTagView.isHidden = false
            })
            
            return _btn
        }
    }()
    
    /// 顶部指标视图
    private lazy var topView:UIView = {
        autoreleasepool {
            let _v = BaseView.createView(rect: .zero)
            _v.isHidden = true
            
            //MARK:ma5
            let _ma5p = BaseView.createView(rect: .init(x: 0, y: 0, width: 5, height: 5),
                                            backgroundColor: MarketSetting.dif_color,
                                            isRadius: true,
                                            cornerRadius: 2.5,
                                            andBorder: 0, andBorderColor: nil)
            _ma5p.tag = 11
            _v.addSubview(_ma5p)
            _ma5p.snp.makeConstraints { make in
                make.left.equalTo(10)
                make.centerY.equalTo(_v.snp.centerY)
                make.width.height.equalTo(5)
            }
            
            var tag = 1
            let _ma5 = BaseView.createLable(rect: .zero,
                                            text: "MA5:0.00",
                                            textColor: UIColor.init().colorFromHexInt(hex: 0x333333, alpha: 0.5),
                                            font: UIFont.systemFont(ofSize: 12))
            _ma5.textAlignment = .left
            _ma5.tag = tag
            
            _v.addSubview(_ma5)
            _ma5.snp.makeConstraints { make in
                make.left.equalTo(_ma5p.snp.right)
                make.centerY.equalTo(_v.snp.centerY)
                make.width.equalTo((Setting.K_APP_WIDTH - 30)/4)
            }
            
            //MARK:ma10
            let _ma10p = BaseView.createView(rect: .init(x: 0, y: 0, width: 5, height: 5),
                                             backgroundColor: MarketSetting.dea_color,
                                             isRadius: true,
                                             cornerRadius: 2.5,
                                             andBorder: 0, andBorderColor: nil)
            _ma10p.tag = 22
            _v.addSubview(_ma10p)
            _ma10p.snp.makeConstraints { make in
                make.left.equalTo(_ma5.snp.right)
                make.centerY.equalTo(_v.snp.centerY)
                make.width.height.equalTo(5)
            }
            
            tag = 2
            let _ma10 = BaseView.createLable(rect: .zero,
                                             text: "MA10:0.00",
                                             textColor: UIColor.init().colorFromHexInt(hex: 0x333333, alpha: 0.5),
                                             font: UIFont.systemFont(ofSize: 12))
            _ma10.textAlignment = .left
            _ma10.tag = tag
            
            _v.addSubview(_ma10)
            _ma10.snp.makeConstraints { make in
                make.left.equalTo(_ma10p.snp.right)
                make.centerY.equalTo(_v.snp.centerY)
                make.width.equalTo((Setting.K_APP_WIDTH - 30)/4)
            }
            
            //MARK:ma15
            let _ma15p = BaseView.createView(rect: .init(x: 0, y: 0, width: 5, height: 5),
                                             backgroundColor: MarketSetting.kmacd_color,
                                             isRadius: true,
                                             cornerRadius: 2.5,
                                             andBorder: 0, andBorderColor: nil)
            _ma15p.tag = 33
            _v.addSubview(_ma15p)
            _ma15p.snp.makeConstraints { make in
                make.left.equalTo(_ma10.snp.right)
                make.centerY.equalTo(_v.snp.centerY)
                make.width.height.equalTo(5)
            }
            
            tag = 3
            let _ma15 = BaseView.createLable(rect: .zero,
                                             text: "MA15:0.00",
                                             textColor: UIColor.init().colorFromHexInt(hex: 0x333333, alpha: 0.5),
                                             font: UIFont.systemFont(ofSize: 12))
            _ma15.textAlignment = .left
            _ma15.tag = tag
            
            _v.addSubview(_ma15)
            _ma15.snp.makeConstraints { make in
                make.left.equalTo(_ma15p.snp.right)
                make.centerY.equalTo(_v.snp.centerY)
                make.width.equalTo((Setting.K_APP_WIDTH - 30)/4)
            }
            
            //MARK:ma30
            let _ma30p = BaseView.createView(rect: .init(x: 0, y: 0, width: 5, height: 5),
                                             backgroundColor: MarketSetting.kother_color,
                                             isRadius: true,
                                             cornerRadius: 2.5,
                                             andBorder: 0,
                                             andBorderColor: nil)
            _ma30p.tag = 44
            _v.addSubview(_ma30p)
            _ma30p.snp.makeConstraints { make in
                make.left.equalTo(_ma15.snp.right)
                make.centerY.equalTo(_v.snp.centerY)
                make.width.height.equalTo(5)
            }
            
            tag = 4
            let _ma30 = BaseView.createLable(rect: .zero,
                                             text: "MA30:0.00",
                                             textColor: UIColor.init().colorFromHexInt(hex: 0x333333, alpha: 0.5),
                                             font: UIFont.systemFont(ofSize: 12))
            _ma30.textAlignment = .left
            _ma30.tag = tag
            
            _v.addSubview(_ma30)
            _ma30.snp.makeConstraints { make in
                make.left.equalTo(_ma30p.snp.right)
                make.centerY.equalTo(_v.snp.centerY)
                make.width.equalTo((Setting.K_APP_WIDTH - 30)/4)
            }
            
            return _v
        }
    }()
    
    //MARK: 混合副图指标
    private lazy var combineLineChare:CombinedChartView = {[weak self] in
        autoreleasepool {
            let _ccv = CombinedChartView.init()
            
            //设置动画时间
            _ccv.animate(xAxisDuration:MarketSetting.timeInterval)
            
            //整体样式
            MarketSetting.setGradStyleFor(Chart: _ccv,
                                          withEdgeInsets: .init(top: 0, left: 10, bottom: 0, right:-1.5),
                                          andDelegate:self)
            
            //底部横轴样式
            MarketSetting.setBottomCalibrationStyleFor(Chart: _ccv,
                                                       andIsShow: false,
                                                       withShowCount: 3)
            
            //缩放样式
            MarketSetting.setZoomStyleFor(Chart: _ccv)
            
            //空视图样式
            MarketSetting.setEmptyStyleFor(Chart: _ccv)
            
            //右边刻度样式
            MarketSetting.setRightCalibrationStyleFor(Chart: _ccv, andShowCount: 3)
            
            return _ccv
        }
    }()
    
    //MACD标题
    private lazy var btnMACDTitle:UIButton = {
        autoreleasepool {
            let _btn = BaseView.createBtn(rect: .zero,
                                          strTitle: "MACD(12,26,9)",
                                          titleColor: UIColor.init().colorFromHexInt(hex: 0x333333, alpha: 0.5),
                                          txtFont: UIFont.systemFont(ofSize: 12),
                                          image: nil,
                                          backgroundColor: nil,
                                          borderColor: nil,
                                          cornerRadius: nil,
                                          isRadius: false,
                                          backgroundImage: nil,
                                          borderWidth: nil)
            
            _btn.contentHorizontalAlignment = .left
            
            _btn.rac_signal(for: .touchUpInside)?.subscribeNext({ (_:Any?) in
                self.bottomTagView.isHidden = false
            })
            
            return _btn
        }
    }()
    
    
    /// 副图指标切换
    private lazy var bottomTagView:UIView = {
        autoreleasepool {
            let _v = BaseView.createView(rect: .zero,
                                         backgroundColor: UIColor.init().colorFromHexInt(hex: 0x000000,alpha: 0.6),
                                         isRadius: false,
                                         cornerRadius: 0,
                                         andBorder: 0,
                                         andBorderColor: nil)
            
            for i in 0..<MarketSetting.arrMinorKPI.count {
                let _btn = BaseView.createBtn(rect: .zero,
                                              strTitle: MarketSetting.arrMinorKPI[i],
                                              titleColor: UIColor.white,
                                              txtFont: UIFont.systemFont(ofSize: 12),
                                              image: nil,
                                              backgroundColor: nil,
                                              borderColor: nil,
                                              cornerRadius: nil,
                                              isRadius: false,
                                              backgroundImage: nil,
                                              borderWidth: nil)
                
                _btn.tag = i
                _btn.rac_signal(for: .touchUpInside)?.subscribeNext({[weak self] (_btn:Any?) in
                    guard let self = self else { return }
                    guard let btn = _btn as? UIButton else { return }
                    if MarketSetting.minorCurrentIndex == btn.tag {
                        self.bottomTagView.isHidden = true
                        return
                    }
                    
                    MarketSetting.minorCurrentIndex = btn.tag
                    switch btn.tag {
                    case 0:
                        self.btnMACDTitle.setTitle("MACD(12,26,9)", for: .normal)
                        break
                        
                    case 1:
                        self.btnMACDTitle.setTitle("KDJ(9,3,3)", for: .normal)
                        break
                        
                    default:
                        self.btnMACDTitle.setTitle(MarketSetting.arrMinorKPI.count > btn.tag ? MarketSetting.arrMinorKPI[btn.tag] : "未知", for: .normal)
                        break
                    }
                    
                    self.bottomTagView.isHidden = true
                    self.setCombineLineData(isChange: true,andIndex:btn.tag)
                })
                
                _v.addSubview(_btn)
                _btn.snp.makeConstraints { make in
                    make.left.right.equalTo(0)
                    make.height.equalTo(30)
                    make.top.equalTo(_btn.tag * 30)
                }
            }
            
            _v.isHidden = true
            return _v
        }
    }()
    
    
    /// 底部指标视图
    private lazy var bottomView:UIView = {
        autoreleasepool {
            let _v = BaseView.createView(rect: .zero)
            _v.isHidden = true
            
            //MARK:DIF
            let _difp = BaseView.createView(rect: .init(x: 0, y: 0, width: 5, height: 5),
                                            backgroundColor: MarketSetting.dif_color,
                                            isRadius: true,
                                            cornerRadius: 2.5,
                                            andBorder: 0, andBorderColor: nil)
            _v.addSubview(_difp)
            _difp.snp.makeConstraints { make in
                make.left.equalTo(10)
                make.centerY.equalTo(_v.snp.centerY)
                make.width.height.equalTo(5)
            }
            
            var tag = 1
            let _dif = BaseView.createLable(rect: .zero,
                                            text: "DIF:0.00",
                                            textColor: UIColor.init().colorFromHexInt(hex: 0x333333, alpha: 0.5),
                                            font: UIFont.systemFont(ofSize: 12))
            _dif.textAlignment = .left
            _dif.tag = tag
            
            _v.addSubview(_dif)
            _dif.snp.makeConstraints { make in
                make.left.equalTo(_difp.snp.right)
                make.centerY.equalTo(_v.snp.centerY)
                make.width.equalTo((Setting.K_APP_WIDTH - 30)/3)
            }
            
            //MARK:DEA
            let _deap = BaseView.createView(rect: .init(x: 0, y: 0, width: 5, height: 5),
                                            backgroundColor: MarketSetting.dea_color,
                                            isRadius: true,
                                            cornerRadius: 2.5,
                                            andBorder: 0, andBorderColor: nil)
            _v.addSubview(_deap)
            _deap.snp.makeConstraints { make in
                make.left.equalTo(_dif.snp.right)
                make.centerY.equalTo(_v.snp.centerY)
                make.width.height.equalTo(5)
            }
            
            tag = 2
            let _dea = BaseView.createLable(rect: .zero,
                                            text: "DEA:0.00",
                                            textColor: UIColor.init().colorFromHexInt(hex: 0x333333, alpha: 0.5),
                                            font: UIFont.systemFont(ofSize: 12))
            _dea.textAlignment = .left
            _dea.tag = tag
            
            _v.addSubview(_dea)
            _dea.snp.makeConstraints { make in
                make.left.equalTo(_deap.snp.right)
                make.centerY.equalTo(_v.snp.centerY)
                make.width.equalTo((Setting.K_APP_WIDTH - 30)/3)
            }
            
            //MARK:MACD
            let _macdp = BaseView.createView(rect: .init(x: 0, y: 0, width: 5, height: 5),
                                             backgroundColor: MarketSetting.kmacd_color,
                                             isRadius: true,
                                             cornerRadius: 2.5,
                                             andBorder: 0, andBorderColor: nil)
            _v.addSubview(_macdp)
            _macdp.snp.makeConstraints { make in
                make.left.equalTo(_dea.snp.right)
                make.centerY.equalTo(_v.snp.centerY)
                make.width.height.equalTo(5)
            }
            
            tag = 3
            let _macd = BaseView.createLable(rect: .zero,
                                             text: "MACD:0.00",
                                             textColor: UIColor.init().colorFromHexInt(hex: 0x333333, alpha: 0.5),
                                             font: UIFont.systemFont(ofSize: 12))
            _macd.textAlignment = .left
            _macd.tag = tag
            
            _v.addSubview(_macd)
            _macd.snp.makeConstraints { make in
                make.left.equalTo(_macdp.snp.right)
                make.centerY.equalTo(_v.snp.centerY)
                make.width.equalTo((Setting.K_APP_WIDTH - 30)/3)
            }
            
            return _v
        }
    }()
}


//MARK: - 十字线(自定义)
extension TwoMainView {
    
    /// 长按手势
    /// - Parameter sender: UILongPressGestureRecognizer
    @objc private func longGestAction(sender:UILongPressGestureRecognizer) {
        
        let transPoint:CGPoint = sender.location(in: self.combineLineStickChart)
        if transPoint.x > Setting.K_APP_WIDTH - 30 {
            return
        }
        
        print(transPoint.x)
        
        switch sender.state {
            //MARK:开始
        case .began:
            let (_,_newPoint) = self.updatePositionFor(Point: transPoint)
            self.labVerticalLine.center = .init(x: _newPoint.x, y: self.labVerticalLine.size.height * 0.5 + 10)
            
            if transPoint.y >= self.combineLineStickChart.top && transPoint.y <= self.combineLineChare.bottom  {
                self.labHorizontalLine.center = .init(x: self.labHorizontalLine.width * 0.5 + 10, y: transPoint.y)
                self.updateRightViewFor(Point: transPoint)
            }
            
            self.labHorizontalRight.isHidden = false
            self.labHorizontalLine.isHidden = false
            self.labVerticalLine.isHidden = false
            self.bottomView.isHidden = false
            self.topView.isHidden = false
            
            self.btnMACDTitle.isHidden = true
            self.btnMATitle.isHidden = true
            break
            
            //MARK:手势改变
        case .changed:
            let (_m,_newPoint) = self.updatePositionFor(Point: transPoint)
            
            if let _model = _m {
                print(String.init(format: "o:%.2f,h:%.2f,l:%.2f,c:%.2f",_model.open,_model.high,_model.low,_model.close))
                
                self.labOpen.text = String.init(format:"%.2f",_model.open)
                self.labHeight.text = String.init(format:"%.2f",_model.high)
                self.labLow.text = String.init(format:"%.2f",_model.low)
                self.labClose.text = String.init(format:"%.2f",_model.close)
                
                self.labVerticalLine.center = .init(x: _newPoint.x, y: self.labVerticalLine.size.height * 0.5 + 10)
                if transPoint.y >= self.combineLineStickChart.top && transPoint.y <= self.combineLineChare.bottom  {
                    self.labHorizontalLine.center = .init(x: self.labHorizontalLine.width * 0.5 + 10, y: transPoint.y)
                    self.updateRightViewFor(Point: transPoint)
                }
                
                //更新顶部
                self.updateTopViewFor(Index: Int(_model.x))
                
                //更新底部
                self.updateBottomViewFor(Index: Int(_model.x))
            }
            break
            
            //MARK:其他(取消、结束...)
        default:
            self.labHorizontalRight.isHidden = true
            self.labHorizontalLine.isHidden = true
            self.labVerticalLine.isHidden = true
            self.bottomView.isHidden = true
            self.topView.isHidden = true
            
            self.btnMACDTitle.isHidden = false
            self.btnMATitle.isHidden = false
            break
        }
    }
    
    
    /// 根据手势移动的坐标获取对应K线上的坐标
    /// - Parameter p: CGPoint
    /// - Returns: (CandleChartDataEntry?,CGPoint)
    private func updatePositionFor(Point p:CGPoint) -> (CandleChartDataEntry?,CGPoint) {
        let _model:CandleChartDataEntry? = self.combineLineStickChart.getEntryByTouchPoint(point: p) as? CandleChartDataEntry
        
        var _newPoint = p
        if _model != nil {
            _newPoint = self.combineLineStickChart.getPosition(entry: _model!, axis: YAxis.AxisDependency.right)
        }
        
        return (_model,_newPoint)
    }
    
    //MARK:主图指标值
    /// 更新顶部主图指标
    /// - Parameter ix: Int
    private func updateTopViewFor(Index ix:Int) {
        
        if MarketSetting.mainCurrentIndex == 0 {
            guard let(_arrMa5,_arrMa10,_arrMa15,_arrMa30) = (MarketSetting.dicKpiMainData[MA.kpiType.ma5.rawValue],MarketSetting.dicKpiMainData[MA.kpiType.ma10.rawValue],MarketSetting.dicKpiMainData[MA.kpiType.ma15.rawValue],MarketSetting.dicKpiMainData[MA.kpiType.ma30.rawValue]) as? ([Double],[Double],[Double],[Double]) else {
                return
            }
            
            let _count = self.arrChartData.count
            let _5Mindex = _count - _arrMa5.count
            let _10Mindex = _count - _arrMa10.count
            let _15Mindex = _count - _arrMa15.count
            let _30Mindex = _count - _arrMa30.count
            
            self.moveUpdateFor(View: self.topView,
                               andV1: (_arrMa5.count > (ix - _5Mindex) && (ix - _5Mindex) >= 0) ? String.init(format:"MA5:%.2f",_arrMa5[ix - _5Mindex]) : "MA5:0.00",
                               andV2: (_arrMa10.count > (ix - _10Mindex) && (ix - _10Mindex) >= 0) ? String.init(format:"MA10:%.2f",_arrMa10[ix - _10Mindex]) : "MA10:0.00",
                               withV3:(_arrMa15.count > (ix - _15Mindex) && (ix - _15Mindex) >= 0) ? String.init(format:"MA15:%.2f",_arrMa15[ix - _15Mindex]) : "MA15:0.00",
                               withV4: (_arrMa30.count > (ix - _30Mindex) && (ix - _30Mindex) >= 0) ? String.init(format:"MA30:%.2f",_arrMa30[ix - _30Mindex]) : "MA30:0.00")
        }
        else if MarketSetting.mainCurrentIndex == 1 {
            guard let(_arrUp,_arrMid,_arrLow) = (MarketSetting.dicKpiMainData[BOLL.kpiType.up.rawValue],
                                                 MarketSetting.dicKpiMainData[BOLL.kpiType.mid.rawValue],MarketSetting.dicKpiMainData[BOLL.kpiType.low.rawValue]) as? ([Double],[Double],[Double]) else {
                return
            }
            
            let _count = self.arrChartData.count
            let _uMindex = _count - _arrUp.count
            let _mMindex = _count - _arrMid.count
            let _lMindex = _count - _arrLow.count
            
            self.moveUpdateFor(View: self.topView,
                               andV1: (_arrUp.count > (ix - _uMindex) && (ix - _uMindex) >= 0) ? String.init(format:"UP:%.2f",_arrUp[ix - _uMindex]) : "UP:0.00",
                               andV2: (_arrMid.count > (ix - _mMindex) && (ix - _mMindex) >= 0) ? String.init(format:"MID:%.2f",_arrMid[ix - _mMindex]) : "MID:0.00",
                               withV3:(_arrLow.count > (ix - _lMindex) && (ix - _lMindex) >= 0) ? String.init(format:"LOW:%.2f",_arrLow[ix - _lMindex]) : "LOW:0.00",
                               withV4: nil)
        }
        
    }
    
    //MARK:副图指标值
    /// 更新底部副图指标
    /// - Parameter ix: Int
    private func updateBottomViewFor(Index ix:Int) {
        
        if MarketSetting.minorCurrentIndex == 0 {
            guard let(_arrDif,_arrDea,_arrMACD) = (MarketSetting.dicKpiData[MACD.kpiType.dif.rawValue],MarketSetting.dicKpiData[MACD.kpiType.dea.rawValue],MarketSetting.dicKpiData[MACD.kpiType.macd.rawValue]) as? ([Double],[Double],[Double]) else {
                return
            }
            
            self.moveUpdateFor(View:self.bottomView,
                               andV1: _arrDif.count > ix ? String.init(format:"DIF:%.2f",_arrDif[ix]) : "DIF:0.00",
                               andV2: _arrDea.count > ix ? String.init(format:"DEA:%.2f",_arrDea[ix]) : "DEA:0.00",
                               withV3: _arrMACD.count > ix ? String.init(format:"MACD:%.2f",_arrMACD[ix]) : nil,
                               withV4: nil)
        }
        else if MarketSetting.minorCurrentIndex == 1 {
            guard let(_arrK,_arrD,_arrJ) = (MarketSetting.dicKpiData[KDJ.kpiType.k.rawValue],MarketSetting.dicKpiData[KDJ.kpiType.d.rawValue],MarketSetting.dicKpiData[KDJ.kpiType.j.rawValue]) as? ([Double],[Double],[Double]) else {
                return
            }
            
            self.moveUpdateFor(View:self.bottomView,
                               andV1: _arrK.count > ix ? String.init(format:"K:%.2f",_arrK[ix]) : "K:0.00",
                               andV2: _arrD.count > ix ? String.init(format:"D:%.2f",_arrD[ix]) : "D:0.00",
                               withV3: _arrJ.count > ix ? String.init(format:"J:%.2f",_arrJ[ix]) : nil,
                               withV4: nil)
        }
    }
    
    private func moveUpdateFor(View v:UIView,
                               andV1 v1:String,
                               andV2 v2:String,
                               withV3 v3:String? = nil,
                               withV4 v4:String? = nil) {
        var labV1:YYLabel?
        var labV2:YYLabel?
        var labV3:YYLabel?
        var labV4:YYLabel?
        
        for _view in v.subviews {
            switch _view.tag {
            case 1:
                labV1 = _view as? YYLabel
                
            case 2:
                labV2 = _view as? YYLabel
                
            case 3:
                labV3 = _view as? YYLabel
                v.viewWithTag(33)?.isHidden = v3 == nil ? true : false
                
            case 4:
                labV4 = _view as? YYLabel
                v.viewWithTag(44)?.isHidden = v4 == nil ? true : false
                
            default:
                break
            }
        }
        
        labV1?.text = v1
        labV2?.text = v2
        labV3?.text = v3
        labV4?.text = v4
    }
    
    //MARK:十字线数值
    /// 更新右边水平十字线数值
    /// - Parameter p: CGPoint
    private func updateRightViewFor(Point p:CGPoint) {
        self.labHorizontalRight.centerY = self.labHorizontalLine.centerY
        
        var _min:Double = 0.0
        var _max:Double = 0.0
        var _unitValue:Double = 0.0
        var _haveHight:CGFloat = 0.0
        
        //匹配主图刻度值
        if p.y >= self.combineLineStickChart.top && p.y <= self.combineLineStickChart.bottom {
            _min = self.combineLineStickChart.rightAxis.axisMinimum
            _max = self.combineLineStickChart.rightAxis.axisMaximum
            
            //计算单位高度的值
            _unitValue = (_max - _min) / Double(self.combineLineStickChart.height)
            
            //拥有的单位高度
            _haveHight = p.y - self.combineLineStickChart.top
        }
        //匹配附图刻度值
        else if p.y >= self.combineLineChare.top && p.y <= self.combineLineChare.bottom {
            _min = self.combineLineChare.rightAxis.axisMinimum
            _max = self.combineLineChare.rightAxis.axisMaximum
            
            //计算单位高度的值
            _unitValue = (_max - _min) / Double(self.combineLineChare.height)
            
            //拥有的单位高度
            _haveHight = p.y - self.combineLineChare.top
        }
        
        //赋值
        if _max > 0 {
            self.labHorizontalRight.text = String.init(format:"%.2f",_max - _unitValue * Double(_haveHight))
        }
    }
}


//MARK: - 主图数据绑定
extension TwoMainView {
    
    /// 主图数据绑定
    /// - Parameter isChange: Bool
    private func setMainCombineLineData(isChange:Bool = false) {
        
        if self.arrChartData.count <= 0 { return }
        
        //MARK:X轴数据
        var i = 0
        var xValues = [String]()
        var yStockValue = [ChartDataEntry]()
        
        for item in self.arrChartData {
            let _dateTime:UInt32 = item["UpdateTime"] as! UInt32
            let _strDateTime = Utils.shareInstance().getDateTimeForUnix(unixTime: _dateTime, strFormat: "MM/dd HH:mm")
            xValues.append(_strDateTime)
            
            let entry = CandleChartDataEntry.init(x: Double.init(i),
                                                  shadowH: Double.init("\(item["High"] ?? "0.000")")!,
                                                  shadowL: Double.init("\(item["Low"] ?? "0.000")")!,
                                                  open: Double.init("\(item["Open"] ?? "0.000")")!,
                                                  close: Double.init("\(item["Close"] ?? "0.000")")!)
            
            yStockValue.append(entry)
            i += 1
        }
        
        if xValues.count > 0 {
            self.combineLineStickChart.xAxis.valueFormatter = IndexAxisValueFormatter.init(values: xValues)
            self.combineLineChare.xAxis.valueFormatter = IndexAxisValueFormatter.init(values: xValues)
        }
        
        //MARK:MA
        if MarketSetting.mainCurrentIndex == 0 {
            MarketSetting.dicKpiMainData = [
                MA.kpiType.ma5.rawValue :  MA.calcMA(Data: self.arrChartData, andNmbner: 5),
                MA.kpiType.ma10.rawValue : MA.calcMA(Data: self.arrChartData, andNmbner: 10),
                MA.kpiType.ma15.rawValue : MA.calcMA(Data: self.arrChartData, andNmbner: 15),
                MA.kpiType.ma30.rawValue : MA.calcMA(Data: self.arrChartData, andNmbner: 30),
            ]
            
            guard let(_arrMA5,_arrMA10,_arrMA15,_arrMA30) = (MarketSetting.dicKpiMainData[MA.kpiType.ma5.rawValue],
                                                             MarketSetting.dicKpiMainData[MA.kpiType.ma10.rawValue],
                                                             MarketSetting.dicKpiMainData[MA.kpiType.ma15.rawValue],
                                                             MarketSetting.dicKpiMainData[MA.kpiType.ma30.rawValue]) as? (
                                                                [Double],[Double],[Double],[Double]) else {
                return
            }
            
            self.setMainKpiDataFor(Values1: _arrMA5,
                                   Values2: _arrMA10,
                                   Values3: _arrMA15,
                                   Values4: _arrMA30,
                                   withYStockValue: yStockValue,
                                   andisChange: isChange)
        }
        //MARK:BOLL
        else if MarketSetting.mainCurrentIndex == 1 {
            MarketSetting.dicKpiMainData = BOLL.calcBOLLFor(Data: self.arrChartData, andNumber: 26, withP: 2)
            
            guard let(_arrUp,_arrMid,_arrLow) = (MarketSetting.dicKpiMainData[BOLL.kpiType.up.rawValue],
                                                 MarketSetting.dicKpiMainData[BOLL.kpiType.mid.rawValue],
                                                 MarketSetting.dicKpiMainData[BOLL.kpiType.low.rawValue]) as? (
                                                    [Double],[Double],[Double]) else {
                return
            }
            
            self.setMainKpiDataFor(Values1: _arrUp,
                                   Values2: _arrMid,
                                   Values3: _arrLow,
                                   withYStockValue: yStockValue,
                                   andisChange: isChange)
        }
        
    }
    
    
    private func setMainKpiDataFor(Values1 _arrV1:[Double],
                                   Values2 _arrV2:[Double],
                                   Values3 _arrV3:[Double],
                                   Values4 _arrV4:[Double]? = nil,
                                   withYStockValue yStockValue:[ChartDataEntry] = [ChartDataEntry](),
                                   andisChange isChange:Bool = false){
        
        var yV1  = [ChartDataEntry]()
        var yV2  = [ChartDataEntry]()
        var yV3  = [ChartDataEntry]()
        var yV4  = [ChartDataEntry]()
        
        //V1
        let _count = self.arrChartData.count
        var minIndex = self.arrChartData.count - _arrV1.count
        for i in minIndex..<_count {
            let entry = ChartDataEntry.init(x: Double.init(i),y: _arrV1[i - minIndex])
            yV1.append(entry)
        }
        
        //V2
        minIndex = self.arrChartData.count - _arrV2.count
        for i in minIndex..<_count {
            let entry = ChartDataEntry.init(x: Double.init(i),y: _arrV2[i - minIndex])
            yV2.append(entry)
        }
        
        //V3
        minIndex = self.arrChartData.count - _arrV3.count
        for i in minIndex..<_count {
            let entry = ChartDataEntry.init(x: Double.init(i),y: _arrV3[i - minIndex])
            yV3.append(entry)
        }
        
        //V4
        if _arrV4 != nil {
            minIndex = self.arrChartData.count - _arrV4!.count
            for i in minIndex..<_count {
                let entry = ChartDataEntry.init(x: Double.init(i),y: _arrV4![i - minIndex])
                yV4.append(entry)
            }
        }
        
        //设置数据
        let data:CombinedChartData = CombinedChartData.init()
        
        var ds = [
            MarketSetting.setLineDataFor(Values: yV1,andColor: MarketSetting.dif_color,withName: ""),
            MarketSetting.setLineDataFor(Values: yV2,andColor: MarketSetting.dea_color,withName: ""),
            MarketSetting.setLineDataFor(Values: yV3,andColor: MarketSetting.kmacd_color,withName: ""),
        ]
        
        if _arrV4 != nil {
            ds.append(MarketSetting.setLineDataFor(Values: yV4, andColor: MarketSetting.kother_color,withName:""))
        }
        
        data.lineData = LineChartData.init(dataSets: ds)
        
        //股票
        data.candleData = CandleChartData.init(dataSets:[
            MarketSetting.setStockDataSetFor(Values: yStockValue)
        ])
        
        self.combineLineStickChart.data = data
        if isChange {
            self.combineLineStickChart.animate(xAxisDuration: MarketSetting.timeInterval)
        }
        
        if let _xMax = self.combineLineStickChart.data?.xMax {
            //开局移动到最右边xMax为最大x轴值
            self.combineLineStickChart.moveViewToX(_xMax)
        }
    }
}


//MARK: - 副图指标数据绑定
extension TwoMainView {
    
    /// 指标图数据绑定
    /// - Parameter isChange: isChange description
    private func setCombineLineData(isChange:Bool = false,
                                    andIndex ix:Int = 0){
        
        //副图指标数据
        if self.arrChartData.count > 0 {
            
            //MARK:MACD
            if ix == 0 {
                self.combineLineChare.setExtraOffsets(left: 10,top: 0,right: -1.5,bottom: 0)
                MarketSetting.dicKpiData = MACD.calcMACDFor(Data: self.arrChartData)
                guard let(_arrDif,_arrDea,_arrMACD) = (
                    MarketSetting.dicKpiData[MACD.kpiType.dif.rawValue],
                    MarketSetting.dicKpiData[MACD.kpiType.dea.rawValue],
                    MarketSetting.dicKpiData[MACD.kpiType.macd.rawValue]) as? (
                        [Double],[Double],[Double]) else {
                    return
                }
                
                self.setMACDFor(DIF: _arrDif, andDEA: _arrDea, andMACD: _arrMACD,withChange: isChange)
            }
            //MARK:KDJ
            else if ix == 1 {
                self.combineLineChare.setExtraOffsets(left: 0,top: 0,right: 5,bottom: 0)
                MarketSetting.dicKpiData = KDJ.calcKDJFor(Data: self.arrChartData,andNumber: 9,andK: 3,withD: 3)
                guard let(_arrK,_arrD,_arrJ) = (
                    MarketSetting.dicKpiData[KDJ.kpiType.k.rawValue],
                    MarketSetting.dicKpiData[KDJ.kpiType.d.rawValue],
                    MarketSetting.dicKpiData[KDJ.kpiType.j.rawValue]) as? (
                        [Double],[Double],[Double]) else {
                    return
                }
                
                self.setKDJFor(K: _arrK, andD: _arrD, andJ: _arrJ, withChange: isChange)
            }
        }
    }
    
    private func setMACDFor(DIF _arrDif:[Double],
                            andDEA _arrDea:[Double],
                            andMACD _arrMACD:[Double],
                            withChange isChange:Bool){
        
        var yDIFValues  = [ChartDataEntry]()
        var yDEAValues  = [ChartDataEntry]()
        var yMACDValues = [ChartDataEntry]()
        
        //DIF
        for i in 0..<_arrDif.count {
            let entry = ChartDataEntry.init(x: Double.init(i),
                                            y: _arrDif[i])
            yDIFValues.append(entry)
        }
        
        //DEA
        for i in 0..<_arrDea.count {
            let entry = ChartDataEntry.init(x: Double.init(i),
                                            y: _arrDea[i])
            yDEAValues.append(entry)
        }
        
        //MACD
        for i in 0..<_arrMACD.count {
            let entry = BarChartDataEntry.init(x: Double.init(i), y: _arrMACD[i])
            yMACDValues.append(entry)
        }
        
        //设置数据
        let data:CombinedChartData = CombinedChartData.init()
        
        //DIF,DEA
        let lineDS = LineChartData.init(dataSets: [
            MarketSetting.setLineDataFor(Values: yDIFValues,
                                         andColor: MarketSetting.dif_color,
                                         withName: "DIF"),
            MarketSetting.setLineDataFor(Values: yDEAValues,
                                         andColor: MarketSetting.dea_color,
                                         withName: "DEA")])
        data.lineData = lineDS
        
        //MACD
        data.barData = BarChartData.init(dataSets: [
            MarketSetting.setBarCharSetFor(Values: yMACDValues, withName: "MACD")
        ])
        data.barData.barWidth = 0.75
        self.combineLineChare.data = data
        
        if isChange {
            self.combineLineChare.animate(xAxisDuration: MarketSetting.timeInterval)
        }
        
        if let _xMax = self.combineLineChare.data?.xMax {
            //开局移动到最右边xMax为最大x轴值
            self.combineLineChare.moveViewToX(_xMax)
        }
    }
    
    private func setKDJFor(K _arrK:[Double],
                           andD _arrD:[Double],
                           andJ _arrJ:[Double],
                           withChange isChange:Bool){
        
        var yKValues  = [ChartDataEntry]()
        var yDValues  = [ChartDataEntry]()
        var yJValues  = [ChartDataEntry]()
        
        //K
        for i in 0..<_arrK.count {
            let entry = ChartDataEntry.init(x: Double.init(i),
                                            y: _arrK[i])
            yKValues.append(entry)
        }
        
        //D
        for i in 0..<_arrD.count {
            let entry = ChartDataEntry.init(x: Double.init(i),
                                            y: _arrD[i])
            yDValues.append(entry)
        }
        
        //J
        for i in 0..<_arrJ.count {
            let entry = BarChartDataEntry.init(x: Double.init(i),
                                               y: _arrJ[i])
            yJValues.append(entry)
        }
        
        //设置数据
        let data:CombinedChartData = CombinedChartData.init()
        
        
        let lineDS = LineChartData.init(dataSets: [
            MarketSetting.setLineDataFor(Values: yKValues,
                                         andColor: MarketSetting.dif_color,
                                         withName: "K"),
            MarketSetting.setLineDataFor(Values: yDValues,
                                         andColor: MarketSetting.dea_color,
                                         withName: "D"),
            MarketSetting.setLineDataFor(Values: yJValues,
                                         andColor: MarketSetting.kmacd_color,
                                         withName: "J"),
        ])
        data.lineData = lineDS
        
        self.combineLineChare.data = data
        if isChange {
            self.combineLineChare.animate(xAxisDuration: MarketSetting.timeInterval)
        }
        
        if let _xMax = self.combineLineChare.data?.xMax {
            //开局移动到最右边xMax为最大x轴值
            self.combineLineChare.moveViewToX(_xMax)
        }
    }
    
}


//MARK: - ChartViewDelegate
extension TwoMainView : ChartViewDelegate {
    
    
    /// 图表被移动(主、副图同时响应)
    /// https://blog.csdn.net/dianchidu6913/article/details/101228178
    /// - Parameters:
    ///   - chartView: ChartViewBase
    ///   - dX: CGFloat
    ///   - dY: CGFloat
    func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat) {
        self.updateTouchFor(ChartView: chartView)
    }
    
    
    /// 图表被缩放
    /// - Parameters:
    ///   - chartView: ChartViewBase
    ///   - scaleX: CGFloat
    ///   - scaleY: CGFloat
    func chartScaled(_ chartView: ChartViewBase, scaleX: CGFloat, scaleY: CGFloat) {
        self.updateTouchFor(ChartView: chartView)
    }
    
    
    /// 同步手势
    /// - Parameter chartView: chartView description
    private func updateTouchFor(ChartView chartView:ChartViewBase){
        let srcMatrix:CGAffineTransform = chartView.viewPortHandler.touchMatrix
        
        self.combineLineStickChart.viewPortHandler.refresh(newMatrix: srcMatrix,
                                                           chart: self.combineLineStickChart,
                                                           invalidate: true)
        
        self.combineLineChare.viewPortHandler.refresh(newMatrix: srcMatrix,
                                                      chart: self.combineLineChare,
                                                      invalidate: true)
    }
}

